<?php

class AttachmentModel
{
	private $attachmentKeyPrefix   =   'AM/';
	
	public function addAttachment($info)/*{{{*/
	{
		if (empty($info['tid']) || empty($info['uid']))
		{
			AC::$errorNo    =   'AMM001';
			return  FALSE;
		}
		
		$data['uid']      	=   $info['uid'];
		$data['tid']   		=   $info['tid'];
		$data['pid']   		=   $info['pid'];
		$data['uploadTime']	=   AC::SERVER_TIME;
		// 附件类型 1= 图片 2= 语音
		$data['attachmentType'] =   $info['type'];
		
		$key    =   $this->attachmentKeyPrefix. $info['uid'];
		
		foreach ($info['attachment']   as  $var)
		{
			$data['attachmentPath'] =   $var['path'];
			isset($var['info']) && $data['info'] = $var['info'];
			isset($var['extra']) && $data['extra'] = $var['extra'];
			// 图片附件默认为1
			if (empty($data['attachmentType'])){
			    $data['attachmentType']  = 1;
			}
			Storage::addData($key, $data);
		}
		$data = array();
		foreach ($info['voice']   as  $var)
		{
		    $data['attachmentPath'] =   $var['path'];
		    isset($var['info']) && $data['info'] = $var['info'];
		    isset($var['extra']) && $data['extra'] = $var['extra'];
		    // 图片附件默认为1
		    if (empty($data['attachmentType'])){
		        $data['attachmentType']  = 1;
		    }
		    Storage::addData($key, $data);
		}
		return  TRUE;
	}/*}}}*/
	
	public function removeAttachment($info)/*{{{*/
	{
		if (empty($info['uid']) || empty($info['tid']))
		{
			AC::$errorNo    =   'AMM001';
			return  FALSE;
		}
	
		$data   =   array('status' => 1);
		$key    =   $this->attachmentKeyPrefix. $info['uid'];
		$where  =   array('tid' => $info['tid'],'pid'=>$info['pid']);
		$result =   Storage::setConditionData($key, $data, $where);
		if (empty($result))
		{
			AC::$errorNo    =   'AMM003';
			return  FALSE;
		}
		return  $result;
	}/*}}}*/
	/**
	 * 获取附件,1=图片,2=语音
	 * @param unknown $info
	 * @return boolean|Ambigous <multitype:multitype: , multitype:unknown >
	 */
	public function getAttachmentInfo($info)/*{{{*/
	{
		if (empty($info['tid']))
		{
			AC::$errorNo = 'AMM001';
			return FALSE;
		}
	
		$key    =   $this->attachmentKeyPrefix. $info['uid'];
		$where  =   array('tid' => $info['tid'],'pid'=>$info['pid'],'status' => 0);
		$result =   Storage::getConditionManyData($key, $where);
		$list   =   array('voiceList'=>array(),'attachmentList'=> array());
		if (!empty($result)){
		    foreach ($result as $value){
		        if (intval($value['attachmentType']) == 2){
		            $list['voiceList'][]    =    $this->attachData($value);
		            continue;
		        }
		        $list['attachmentList'][]    =    $this->attachData($value);
		    }
		}
		
		return $list;
	}/*}}}*/
	
	// 附件返回数据
	private function attachData($info)/*{{{*/
	{
		$data = array();
		$data['id']					= $info['id'];
		$data['uid']				= $info['uid'];
		$data['bodyId']				= $info['tid'];
		$data['info']				= $info['info'];
		$data['uploadTime']			= $info['uploadTime'];
		$data['attachmentType']		= $info['attachmentType'];
		$data['attachmentPath']		= $info['attachmentPath'];
		return $data;
	}
}